package com.wd.live.module_login.view.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.view.View
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.tencent.mm.opensdk.utils.Log
import com.wd.base.bean.LabelsBean
import com.wd.live.module_base.net.bean.login.LoginBean
import com.wd.live.module_base.net.util.MMKVUtil
import com.wd.live.module_base.ui.base.BaseActivity
import com.wd.live.module_login.R
import com.wd.live.module_login.databinding.ActivityLoginBinding
import com.wd.live.module_login.viewmodel.LoginViewModel
import java.nio.charset.StandardCharsets


@Route(path = "/login/login_activity")
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {
    //登录的数据
//    val map24 = mapOf<String, String>("userName" to "王晶晶","passwd" to "a123456")
    val map18 = mapOf<String, String>("userName" to "fearaaa","passwd" to "a123456")
    lateinit var labLogin:LoginBean
//    private var labsLogin= mutableListOf<LoginBean>()
    override fun getLayout(): Int {
        return R.layout.activity_login
    }
//    private var labs = mutableListOf<LabelsBean>()
    override fun initData() {
        //普通用户登录 获取到userId sessionId
        binding.loginPu.setOnClickListener(View.OnClickListener {
            viewModel.postLogin(map18)
            viewModel.dataLogin.observe(this){
                it.result?.let {
                    //添加到集合
                    it2->labLogin=it2
                }
                Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show();
                if (it.status=="0000"){
//                    ARouter.getInstance().build("/main/main_activity").navigation()
//                    MMKVUtil.getMMkv().putInt("userId",labLogin.userId)
                    //登录成功存储
                    MMKVUtil.setIsLogin(true)
                    MMKVUtil.setUser(userId = labLogin.userId, sessionId = labLogin.sessionId)
                    MMKVUtil.setUserContent(nickName = labLogin.nickName, headPic = labLogin.headPic, sex = labLogin.sex)
                    ARouter.getInstance().build("/reverso/context_activity").navigation()
                    finish()
                }
            }
        })
//        viewModel.getLabelsData()
//        viewModel.data.observe(this) {
//            it.result?.let { it1 -> labs.addAll(it1) }
////            binding.text.text = labs[0].name
//            Log.i("TAG---", "initData: "+labs[0].name)
//        }
        //查看用户协议
        binding.loginUser.setOnClickListener(View.OnClickListener {
            startActivity(Intent(this@LoginActivity,UserAgreementActivity::class.java))
        })
        //微信登录
        //  调用微信登录方法
        regToWx();
        //        6、完成微信登录
        //        6、完成微信登录

        binding.wxLogin.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View) {
                val b = hasWX()
                if (b) {
                    Toast.makeText(this@LoginActivity, "安装微信了", Toast.LENGTH_SHORT).show()
                    run {

                        // send oauth request
                        val req = SendAuth.Req()
                        req.scope = "snsapi_userinfo" // 只能填 snsapi_userinfo
                        req.state = "wechat_sdk_demo_test"
                        api!!.sendReq(req)
                    }
                    finish()
                } else {
                    Toast.makeText(this@LoginActivity, "没有安装微信", Toast.LENGTH_SHORT).show()
                }
            }
        })
    }

    // APP_ID 替换为你的应用从官方网站申请到的合法appID
    private val APP_ID = "wx064904aa8809f5aa"

    // IWXAPI 是第三方app和微信通信的openApi接口
    private var api: IWXAPI? = null

    private fun regToWx() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, true)

        // 将应用的appId注册到微信
        api?.registerApp(APP_ID)

        //建议动态监听微信启动广播进行注册到微信
        registerReceiver(object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {

                // 将该app注册到微信
                api?.registerApp(APP_ID)
            }
        }, IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP))
    }

    //判断是否安装微信的方法
    fun hasWX(): Boolean {
        val installedPackages = packageManager.getInstalledPackages(0)
        for (i in installedPackages.indices) {
            val packageName = installedPackages[i].packageName
            if (packageName == "com.tencent.mm") {
                return true
            }
        }
        return false
    }
    override fun getViewModelClass(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

}


